package world.criver.h50909.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import world.criver.h50909.dao.daoUpdate;
import world.criver.h50909.entity.entConfig;
import world.criver.h50909.entity.entEquipmentInfo;
import world.criver.h50909.function.funSaveImage;
import world.criver.h50909.request.reqCompany;
import world.criver.h50909.request.reqEquipment;
import world.criver.h50909.response.resStatus;
import world.criver.h50909.tool.toolConfig;
import world.criver.h50909.tool.toolEquipmentInfo;
import world.criver.h50909.tool.toolModelInfo;

import java.util.Base64;

@Service
public class serUpdate implements daoUpdate {
    @Autowired
    private toolConfig toolConfig;
    @Autowired
    private toolEquipmentInfo toolEquipmentInfo;
    @Autowired
    private toolModelInfo toolModelInfo;
    @Autowired
    private funSaveImage funSaveImage;
    @Override
    public resStatus updateCompany(reqCompany info) {
        if(info.getCompany()==null)return new resStatus(toolConfig.updateLogo(info.getImage()));
        return new resStatus(toolConfig.updateName(info.getCompany()));
    }
    @Override
    public resStatus updateEquipment(reqEquipment info) {
        info.setImage(new String(Base64.getDecoder().decode(info.getImage())));
        boolean old=info.getImage().indexOf("id_")==0;
        String image=old?info.getImage().replace("id_",""):funSaveImage.save(info.getImage())+"";
        entEquipmentInfo en=new entEquipmentInfo(info.getName(), info.getModel(),image,info.getMethod()
                ,info.getProtocol(),info.getUrl(),info.getAlarmKey(),info.getProcessKey(),info.getAwaitKey()
                ,info.getProgramKey(),info.getSpeedKey(),System.currentTimeMillis());
        if(info.isCreate())return new resStatus(toolEquipmentInfo.add(en));
        else return new resStatus(toolEquipmentInfo.update(en,info.getOldName()));
    }

    @Override
    public resStatus updateModel(reqEquipment info) {
        info.setImage(new String(Base64.getDecoder().decode(info.getImage())));
        boolean old=info.getImage().indexOf("id_")==0;
        String image=old?info.getImage().replace("id_",""):funSaveImage.save(info.getImage())+"";
        entEquipmentInfo en=new entEquipmentInfo(info.getName(), info.getModel(), image,info.getMethod()
                ,info.getProtocol(),info.getUrl(),info.getAlarmKey(),info.getProcessKey(),info.getAwaitKey()
                ,info.getProgramKey(),info.getSpeedKey(),System.currentTimeMillis());
        if(info.isCreate())return new resStatus(toolModelInfo.add(en));
        else return new resStatus(toolModelInfo.update(en,info.getOldName()));
    }
}
